<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="search" :model="search" :inline="true" label-width="98px">
            <el-form-item label="挂号项目名称" prop="regItemName">
                <el-input
                        v-model="search.regItemName"
                        placeholder="请输入挂号项目名称"
                        clearable
                        size="small"
                        style="width:240px"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="search.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:240px">
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="searchData">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="clearsearch">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->
        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->
        <!-- 数据表格开始 -->
        <el-table border :data="registeredItemTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="挂号项目ID" align="center" prop="regItemId" />
            <el-table-column label="挂号项目名称" align="center" prop="regItemName" />
            <el-table-column label="挂号项目费用" align="center" prop="regItemFee" />
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->
        <!--  分页-->
        <el-pagination
                v-show="page.total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[3, 4, 5]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
        <!--分页结束-->
        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="600px"
                center
                append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="挂号项目名称" prop="regItemName">
                    <el-input v-model="form.regItemName" placeholder="请输入挂号项目名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="挂号项目金额" prop="regItemFee">
                    <el-input-number v-model="form.regItemFee" placeholder="请输入挂号项目金额" clearable size="small" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue"
                        >{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->

    </div>
</template>

<script>
    export default {
        name: "RegistrationFee",
        data(){
            return{
                //条件查询
                search:{},
                //系统状态
                statusOptions:[],
                //表单数据
                registeredItemTableList:[],
                //禁用按钮
                multiple:true,
                // 选中数组
                ids: [],
                //标题
                title:'',
                //对话框
                open:false,
                //表单数据
                form:{},
                // 表单校验
                rules: {
                    regItemName: [
                        { required: true, message: '挂号项目名称不能为空', trigger: 'blur' }
                    ],
                    regItemFee: [
                        { required: true, message: '挂号项目金额不能为空', trigger: 'blur' }
                    ]
                },
                //分页
                page:{
                    current:1,
                    size:3,
                    total:10
                },
            }
        },
        methods:{
            //条件查询
            searchData(){
                this.page.current=1;
                this.initRegistrationFee();
            },
            //重置查询
            clearsearch(){
                this.search={};
                this.initRegistrationFee();
            },
            //添加一条数据
            handleAdd(){
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="添加挂号费用设置信息";
                this.form={regItemFee:0,status:'0'}
                this.open=true;
            },
            //行内修改
            handleUpdate(row){
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="修改挂号费用设置信息";
                this.form =JSON.parse(JSON.stringify(row));
                this.form.regItemId=row.regItemId;
                this.open=true;
            },
            //提交添加修改表单
            handleSubmit(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$axios.post("/system/api/registered/addOrUpdRegistered",this.form).then(r=>{
                            if (r.data.t){
                                this.$message.success(r.data.msg);
                            }else {
                                this.$message.error("操作失败");
                            }
                            this.initRegistrationFee();
                            this.open=false;
                            this.title='';
                            this.form={};
                        })
                    }
                });
            },
            //取消表单提交
            cancel(){
                this.form={};
                this.open=false;
            },
            //多选框触发事件
            handleSelectionChnage(selectRegisteredId){
                this.ids=selectRegisteredId.map(item=>item.regItemId);
                this.multiple = !selectRegisteredId.length;
            },
            //行内删除
            handleDelete(row){
                const regItemIds=row.regItemId || this.ids;
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("/system/api/registered/delRegistered/"+regItemIds).then(r=>{
                        if (r.data.t){
                            this.$message.success(r.data.msg);
                        }else {
                            this.$message.error("操作失败");
                        }
                        this.page.current=1
                        this.initRegistrationFee();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //查询表单数据
            initRegistrationFee(){
                const newparams=Object.assign(this.search,this.page)
                this.$axios.post("/system/api/registered/getAllData",newparams).then(r=>{
                    this.registeredItemTableList=r.data.t.records;
                    this.page.total=r.data.t.total;
                })
            },
            //每页显示的条数
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.page.size = val;
                this.initRegistrationFee();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page.current = val;
                this.initRegistrationFee();
            },
            //加载系统状态
            initStatus() {
                this.$axios.get("system/api/dict/data/findByType/sys_normal_disable").then(r => {
                    this.statusOptions = r.data.t;
                })
            },
            //解析系统状态格式
            statusFormatter(row){
                var val=row.status;
                return this.formatDict(this.statusOptions,val)
            },

        },
        created() {
            //加载初始化表单数据
            this.initRegistrationFee();
            //加载系统状态
            this.initStatus();
        }
    }
</script>

<style scoped>

</style>
